package com.data.monitoring.controller;

import com.data.monitoring.model.entity.MonitoredFolders;
import com.data.monitoring.service.MonitoredFoldersService;
import com.data.monitoring.service.impl.FolderMonitorScheduler;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/monitoring")
@Api(tags = "监听任务控制器")
public class MonitoringController {
    
    @Autowired
    private FolderMonitorScheduler monitorScheduler;
    
    @Autowired
    private MonitoredFoldersService folderRepository;
    
    /**
     * 更新监控间隔
     */
    @PutMapping("/{folderId}/interval")
    public ResponseEntity<?> updateInterval(
            @PathVariable Long folderId,
            @RequestParam Integer interval) {
        
        if (interval < 30) {
            return ResponseEntity.badRequest().body("检查间隔不能小于30秒");
        }
        
        monitorScheduler.updateCheckInterval(folderId, interval);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 立即执行一次检查
     */
    @PostMapping("/{folderId}/check-now")
    public ResponseEntity<?> checkNow(@PathVariable Long folderId) {
        MonitoredFolders folder = folderRepository.getById(folderId);
        if (folder == null) {
            return ResponseEntity.notFound().build();
        }
        monitorScheduler.cancelMonitor(folderId);
        monitorScheduler.scheduleMonitor(folder);
        
        return ResponseEntity.ok().build();
    }
}